var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isUse: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var iconhgt = (res.windowWidth - 20) * 252 / 681;
        that.setData({
          iconhgt: iconhgt
        });
      },
    });

    var validCoupons = [],invalidCoupons = [];
    var couponlist = JSON.parse(options.coupon);
    console.log(couponlist);
    for (var i = 0; i < couponlist.AVAILABLEPROMOTIONS.length;i++){
      for (var keystr in couponlist.AVAILABLEPROMOTIONS[i]) {
        var teminfo = couponlist.AVAILABLEPROMOTIONS[i][keystr];
        teminfo.couponid = keystr;
        validCoupons.push(teminfo);
      }
    }
    for (var i = 0; i < couponlist.DISABLEPROMOTINS.length; i++) {
      for (var keystr in couponlist.DISABLEPROMOTINS[i]){
        var teminfo = couponlist.DISABLEPROMOTINS[i][keystr];
        teminfo.couponid = keystr;
        invalidCoupons.push(teminfo);
      }
    }
    console.log(validCoupons);
    console.log(invalidCoupons);
    this.setData({
      validCoupons: validCoupons,
      invalidCoupons: invalidCoupons
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  selectCoupon:function(e){
    var coupon = e.currentTarget.dataset.coupon;
    var name = coupon.name + coupon.money + '元';
    app.globalData.tempCoupon = {
      id: coupon.couponid,
      name: name
    };
    wx.navigateBack({});
  },
  quitCoupon:function(e){
    app.globalData.tempCoupon = {
      id: null,
      name: ''
    };
    wx.navigateBack({});
  }
})